<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_jiafang/model')->方法名();
*/
namespace jy_jiafang;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	/**
     * 保存商品
     */
	public function save_csv_goods($rows,$merchid=0){
		foreach($rows as $item){
			//$isadd=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'title'=>$item['title']),array('id','title'));
			//if($isadd['title']){
			//	continue;
			//	exi('商品：'.$isadd['title'].',已经存在，请勿重复添加！','error');
			//}
			foreach($this->mi_unit as $k=>$mv){
				if($mv==trim($item['api_data.mi_unit'])){
					$mi_unit=$k;
				}
			}
			foreach($this->dahuo_unit as $k=>$mv){
				if($mv==trim($item['api_data.dahuo_unit'])){
					$dahuo_unit=$k;
				}
			}
			foreach($this->fangz_type as $k=>$mv){
				if($mv==trim($item['api_data.fangz_type'])){
					$fangz_type=$k;
				}
			}
			$newapisaletype=';';
			foreach($this->apisaletype as $k=>$mv){
				if(strpos($item['api_data.saletype'],$mv)!== false){
					$newapisaletype .=$k.';';
				}
			}
			$api_data=array(
				'gong_name'=>$item['api_data.gong_name'],
				'mian_code'=>$item['api_data.mian_code'],
				'gong_code'=>$item['api_data.gong_code'],
				'ku_code'=>$item['api_data.ku_code'],
				'mi_price'=>$item['api_data.mi_price'],
				'mi_unit'=>$mi_unit,
				'dahuo_price'=>$item['api_data.dahuo_price'],
				'dahuo_unit'=>$dahuo_unit,
				'chengfen'=>$item['api_data.chengfen'],
				'menfu'=>$item['api_data.menfu'],
				'midu'=>$item['api_data.midu'],
				'shazhi'=>$item['api_data.shazhi'],
				'fangz_type'=>$fangz_type,
				'remark'=>$item['api_data.remark'],
				'yongtu'=>$item['api_data.yongtu'],
				'apisaletype'=>$newapisaletype,
			);
			$isaddpcate=pdo_get('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['pcate']),'parentid'=>0),array('id','title'));
			if($isaddpcate['id']){
				$pcate=$isaddpcate['id'];
			}else{
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>trim($item['pcate']),
					'isshow'=>1,
				);
				$data['onepluginname']='jy_jiafang';
				$data['pluginname']='jy_jiafang';
				pdo_insert('jy_weishop_goods_group',$data);
				$pcate=pdo_insertid();
			}
			$isaddccate=pdo_get('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['ccate']),'parentid'=>$pcate),array('id','title'));
			if($isaddccate['id']){
				$ccate=$isaddccate['id'];
			}else{
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'title'=>trim($item['ccate']),
					'parentid'=>$pcate,
					'isshow'=>1,
				);
				$data['onepluginname']='jy_jiafang';
				$data['pluginname']='jy_jiafang';
				pdo_insert('jy_weishop_goods_group',$data);
				$ccate=pdo_insertid();
			}
			$param=array(
					array('title'=>'成分','value'=>$api_data['chengfen']),
					array('title'=>'门幅','value'=>$api_data['menfu']),
					array('title'=>'克重','value'=>$item['weight']),
					array('title'=>'纱支','value'=>$api_data['shazhi']),
					array('title'=>'密度','value'=>$api_data['midu']),
					array('title'=>'用途','value'=>$api_data['yongtu']),
					array('title'=>'位置','value'=>$api_data['ku_code']),
					array('title'=>'面料编号','value'=>$api_data['mian_code']),
					array('title'=>'大货价','value'=>$api_data['dahuo_price']),
					array('title'=>'纺织方式','value'=>$item['api_data.fangz_type']),
					array('title'=>'销售类型','value'=>$item['api_data.saletype']),
					array('title'=>'备注','value'=>$api_data['remark']),
			);
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'status'=>1,
				'total'=>188,
				'onepluginname'=>'jy_jiafang',
				'pcate'=>$pcate,
				'ccate'=>$ccate,
				'price'=>$api_data['mi_price'],
				'unit'=>$item['api_data.mi_unit'],
				'title'=>$item['title'],
				'paixu'=>$item['paixu'],
				'weight'=>$item['weight'],
				'api_data'=>ijson_encode($api_data),
				'param'=>ijson_encode($param),
				'type'=>0,
				'actionurl'=>'jy_jiafang/goods.detail/',
			);
			pdo_insert('jy_weishop_goods',$data);
		}
		return true;
	}
	public function get_csvitems($rows){
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[1];
			$rows = array_slice($rows, 2, count($rows) - 2);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv) {
						$item[$cv]=$rv[$cols];
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
	public function save_csv_group($rows,$mid=0){
		foreach($rows as $item){
			$isadd=pdo_get('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'title'=>$item['title']),array('id','title'));
			if($isadd['title']){
				continue;
				exi('分类：'.$isadd['title'].',已经存在','error');
			}
			$data=array(
				'mid'=>$mid,
				'title'=>trim($item['title']),
				'desc'=>trim($item['desc']),
				'image'=>trim($item['image']),
				'advimg'=>trim($item['advimg']),
				'advurl'=>trim($item['advurl']),
				'isshow'=>intval($item['isshow']),
				'paixu'=>intval($item['paixu']),
				'isrecommand'=>intval($item['isrecommand']),
				'parentid'=>intval($item['parentid']),
				'uniacid'=>$_SESSION['uniacid'],
				'onepluginname'=>$item['onepluginname']?:'',
				'pluginname'=>$item['pluginname']?:''
			);
			pdo_insert('jy_weishop_goods_group',$data);
		}
		return true;
	}
	public function save_csv_store($rows){
		foreach($rows as $item){
			$isadd=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'title'=>$item['title']),array('id','title'));
			if($isadd['title']){
				continue;
				exi('门店：'.$isadd['title'].',已经存在，请勿重复添加！','error');
			}
			$data=$item;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_store',$data);
		}
		return true;
	}
	public function save_csv_option($rows){
		foreach($rows as $item){
			$isadd=pdo_get('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['goodsid'],'specs'=>$item['specs']),array('id'));
			if($isadd['id']){
				continue;
				exi('规格：'.$isadd['id'].',已经存在，请勿重复添加！','error');
			}
			$data=$item;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_goods_option',$data);
		}
		return true;
	}
	public function save_csv_spec($rows){
		foreach($rows as $item){
			$isadd=pdo_get('jy_weishop_goods_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['goodsid'],'title'=>$item['title']),array('id','title'));
			if($isadd['title']){
				continue;
				exi('规格：'.$isadd['title'].',已经存在，请勿重复添加！','error');
			}
			$data=$item;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_goods_spec',$data);
		}
		return true;
	}
	public function save_csv_spec_item($rows){
		foreach($rows as $item){
			$isadd=pdo_get('jy_weishop_goods_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['goodsid'],'title'=>$item['title']),array('id','title'));
			if($isadd['title']){
				continue;
				exi('规格：'.$isadd['title'].',已经存在，请勿重复添加！','error');
			}
			$data=$item;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_goods_spec_item',$data);
		}
		return true;
	}
	public function caiji($post){
		if(!$post['goodsid']){
			exi('请输入商品ID','error');
		}
		if($post['type']){
			if(!$post['caiji_api']){
				exi('跨服务器采集，必须填写api','error');
			}
			exi('暂时不支持跨服务器采集，后期升级支持！','error');
			$goods=jy_request($post['caiji_api'],$post);			
		}else{
			$goods=pdo_get('jy_weishop_goods',array('id'=>$post['goodsid']));
		}
		unset($goods['id'],$goods['uniacid'],$goods['mid'],$goods['merchid']);
		$goods['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_goods',$goods);
		return true;
	}
}
?>